package com.example.java_chatroom.controller;

import com.example.java_chatroom.constant.Constants;
import com.example.java_chatroom.mapper.FriendMapper;
import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.UserInfo;
import com.example.java_chatroom.service.FriendService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class FriendController {
    @Autowired
    private FriendService friendService;

    @GetMapping("/getFriendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        //对session进行判空
        if(session==null){
            log.error("[getFriendList]获取session失败");
            return new ArrayList<Friend>();
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION);
        if(userInfo==null){
            log.error("[getFriendList]获取用户失败");
            return new ArrayList<Friend>();
        }
        List<Friend> friendList = friendService.selectFriendList(userInfo.getUserId());
        return friendList;
    }
}
